<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>计算属性和侦听器</title>
    <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
</head>

<body>
    <div id="example">
        <p>原始消息: {{message}}</p>
        <p>翻转消息: {{reverseMessage}}</p>
        <button v-on:click="websocket()">链接websocket</button>
        <p>当前时间:{{nowTime}}</p>
        <div>{{fullName}}</div>
        <!--如果对fullName进行设置,则先触发setter方法 在触发getter方法,其他的则相反-->
        <input type="text" v-model="fullName" >
        <input type="text" v-model="firstName">
        <input type="text" v-model="lastName">

    </div>

    <script>
        console.log(new Vue())
        var vue = new Vue({
            el: '#example',
            data: {
                message: 'Hello Vue',
                firstName: 'James',
                lastName: 'Lei'
            },
            computed: {
                reverseMessage: function () {
                    return this.message.split('').reverse().join('')
                },
                nowTime: function () {
                    return new Date().getFullYear()
                },
                // fullName:function(){
                //     // 直接可以作用到fullName上
                //     return   this.firstName+this.lastName
                // },

                //一般状态下是没有setter方法的 
                fullName: {
                    // getter方法  读值
                    get: function () {
                        console.log('computed getter...')
                        return this.firstName + ' ' + this.lastName
                    },
                    // setter方法 设值
                    set: function (newValue) {
                        console.log('computed setter...')
                        let names = newValue.split(' ')
                        this.firstName = names[0]
                        this.lastName = names[names.length - 1]
                    }
                }
            },
            methods: {
                websocket() {
                    let ws = new WebSocket('wss://test.cytingchechang.com/cloud/ws/CHARGE_FEE_PUSH_77484');
                    ws.onopen = () => {
                            console.log('websocket链接成功')
                        },
                        ws.onmessage = data => {
                            console.log(data)
                            this.sendData({
                                plate: data
                            })
                        },
                        ws.onclose = () => {
                            console.log('websocket已经关闭')
                        }
                },
                sendData(data) {
                    let ajax = new XMLHttpRequest();
                    ajax.open('POST', 'https://test.cytingchechang.com/cloud/wxfee/getOrder');
                    ajax.setRequestHeader('Content-type', "application/x-www-form-urlencoded");
                    ajax.send(data);
                    ajax.onreadystatechange = (res) => {
                        console.log(res)
                    }
                }
            }
        })
    </script>
</body>

</html>